<?php

//公众号获取事件以及回复
class WechatController 
{

    private $postObj;//获取数据



    public function actionIndex()
    {
        $request = \Yii::$app->request;
        $echoStr = $request->get('echostr');
        $signature = $request->get('signature');
        $timestamp = $request->get('timestamp');
        $nonce = $request->get('nonce');

        if (!isset($echoStr)) {
            //返回信息
            $this->responseMsg();
        } else {
            if ($this->checkSignature($signature, $timestamp, $nonce)) {
                echo $echoStr;
            }
        }
    }

    //参数校验
    private function checkSignature($signature, $timestamp, $nonce)
    {
        $token = $_ENV["SNS_WECHAT_MP_TOKEN"];
        if (!$token) {
            echo 'TOKEN is not defined!';
        } else {
            $tmpArr = array($token, $timestamp, $nonce);
            // use SORT_STRING rule
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
            if ($tmpStr == $signature) {
                return true;
            } else {
                return false;
            }
        }
    }

    private function responseMsg()
    {
        $postStr = file_get_contents("php://input");
        if (!empty($postStr)) {
            $this->postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $rxType = trim($this->postObj->MsgType);

            switch ($rxType)
            {
                case "text":
                    $resultStr = $this->receiveText();
                    break;
                case "image":
                    $resultStr = $this->receiveImage();
                    break;
                case "location":
                    $resultStr = $this->receiveLocation();
                    break;
                case "voice":
                    $resultStr = $this->receiveVoice();
                    break;
                case "video":
                    $resultStr = $this->receiveVideo();
                    break;
                case "link":
                    $resultStr = $this->receiveLink();
                    break;
                case "event":
                    $resultStr = $this->receiveEvent();
                    break;
                default:
                    $resultStr = "unknow msg type: ".$rxType;
                    break;
            }
            echo $resultStr;
        } else {
            echo "";
            exit;
        }

    }

    private function receiveText()
    {
        $content = "text";
        return $this->transmitText($content);
    }

    private function receiveImage()
    {

        $content = "image";
        return $this->transmitText($content);
    }

    private function receiveLocation()
    {
        $content = "location";
        return $this->transmitText($content);
    }

    private function receiveVoice()
    {
        $content = "location";
        return $this->transmitText($content);
    }

    private function receiveVideo()
    {
        $content = "voice";
        return $this->transmitText($content);
    }

    private function receiveLink()
    {
        $content = "Link";
        return $this->transmitText($content);
    }

    private function receiveEvent()
    {
        $msgEvent = $this->postObj->Event;
        if ($msgEvent == "CLICK") {//点击菜单
            $eventKey = $this->postObj->EventKey;
            switch ($eventKey)
            {
                case "NEW_PLAN":
                    $content = "新人养成激励计划，敬请期待~";
                    break;
                default:
                    $content = "click event";
            }
        } else {
            $content = "event";
        }
        return $this->transmitText($content);
    }

    private function transmitText($content)
    {
        $textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            <FuncFlag>0</FuncFlag>
            </xml>";
        return sprintf($textTpl, $this->postObj->FromUserName, $this->postObj->ToUserName, time(), 'text', $content);
    }

}